﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Xml.Linq;
using System.Text;
using System.Text.RegularExpressions;

public partial class Server_scripts_Default : System.Web.UI.Page
{
    public struct Parameters
    {
        public string htmlString;
        public int width;
        public int height;
        public string clientPath;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Response.ClearHeaders();
            Response.Clear();
            Response.ClearContent();

            string htmlString = Request["htmlStringValue"];
            string requestBitmapFormat = Request["bitmapFormatValue"];
            int? width = (Regex.IsMatch(Request["widthValue"], "[0-9]") == false) ? 0 : (int?)Convert.ToInt32(Request["widthValue"]);
            int? height = (Regex.IsMatch(Request["heightValue"], "[0-9]") == false) ? 0 : (int?)Convert.ToInt32(Request["heightValue"]);

            System.Drawing.Imaging.ImageFormat bitmatFormat = null;
            switch (requestBitmapFormat.ToUpper())
            {
                case "PNG":
                    bitmatFormat = System.Drawing.Imaging.ImageFormat.Png;
                    break;
                case "GIF":
                    bitmatFormat = System.Drawing.Imaging.ImageFormat.Gif;
                    break;
                case "BMP":
                    bitmatFormat = System.Drawing.Imaging.ImageFormat.Bmp;
                    break;
                case "JPEG":
                    bitmatFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    break;
                default:
                    bitmatFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    break;
            }

            Response.AppendHeader("Content-disposition", string.Format("attachment;filename={0}", "HTMLShotDemo." + requestBitmapFormat.ToLower()));
            Response.ContentType = "application/" + requestBitmapFormat.ToLower();

            HTMLShot.GetHTMLShot(Page.Request, htmlString, Response.OutputStream, bitmatFormat, width, height);
        }
        catch (Exception ex) { }

        Response.End();
    }
}
